﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Common;
using JiveMessenger.Utilities;
using JiveMessenger.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace JiveMessenger.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class CropImagePage : BasicPage
    {
        private StorageFile _loadedFile;
        public CropImagePage()
        {
            this.InitializeComponent();
#if WINDOWS_APP
            BackButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            BackButton.Style = (Style)Application.Current.Resources["NavigationBackButtonNormalStyle"];
#endif
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            ViewModelCropImageBase vm = (ViewModelCropImageBase)this.DataContext;
            vm.Cleanup();


        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            Tuple<ImageCropType, StorageFile> param = e.Parameter as Tuple<ImageCropType, StorageFile>;
            _loadedFile = param.Item2;
            if (param.Item1 == ImageCropType.Avatar)
            {
                this.DataContext = VmHelper.Locator.ViewModelCropAvatarPage;
                this.CropControl.DesiredAspectRatio = 1.0;
                //  this.CropControl.MaxAspectRatio = 2.0;
            }
            else if (param.Item1 == ImageCropType.Emoticon)
            {
                this.DataContext = VmHelper.Locator.ViewModelCropEmoticonPage;
                this.CropControl.DesiredAspectRatio = double.NaN;
                this.CropControl.MaxAspectRatio = 2.5;
            }

        }
        protected override void SaveState(SaveStateEventArgs e)
        {
            ViewModelCropImageBase vm = (ViewModelCropImageBase)this.DataContext;

            e.PageState["ImagePath"] = vm.OriginalImageFile.Path;
            e.PageState["CropHeight"] = vm.CropHeight;
            e.PageState["CropWidth"] = vm.CropWidth;
            e.PageState["CropLeft"] = vm.CropLeft;
            e.PageState["CropRight"] = vm.CropRight;
            e.PageState["CropTop"] = vm.CropTop;
            e.PageState["CropBottom"] = vm.CropBottom;
        }
        protected override async void LoadState(LoadStateEventArgs e)
        {
            if (e.PageState == null || e.PageState.Count == 0)
                return;
            ViewModelCropImageBase vm = (ViewModelCropImageBase)this.DataContext;
            string path = (string)e.PageState["ImagePath"];
            vm.OriginalImageFile = await StorageFile.GetFileFromPathAsync(path);
            vm.CropHeight = (int)e.PageState["CropHeight"];
            vm.CropWidth = (int)e.PageState["CropWidth"];
            vm.CropLeft = (int)e.PageState["CropLeft"];
            vm.CropRight = (int)e.PageState["CropRight"];
            vm.CropTop = (int)e.PageState["CropTop"];
            vm.CropBottom = (int)e.PageState["CropBottom"];
        }

        private void BasicPage_Loaded(object sender, RoutedEventArgs e)
        {
            ((ViewModelCropImageBase)this.DataContext).SetOriginalImage(_loadedFile);
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationHelper.GoBack();
        }
    }
}
